/*******************************************************************************
 * 版权：北京润尼尔网络科技有限公司，保留所有版权<br>
 * 版本：v1.0.0<br>
 * 文件：XYAbsFigureLocator.js<br>
 * 日期：2016年10月21日<br>
 * 作者: 王志远<br>
 * 功能：XY绝对坐标定位器<br>
 * 
 * 修改记录：<br>
 * 日期 描述 更新功能<br>
 ******************************************************************************/

window.makeNamespace(window,"Rainier.UI.V2");

/**
 * 类型名称：Rainier.UI.V2.XYAbsFigureLocator<br>
 * 类型功能：XY绝对坐标定位器<br>
 */
Rainier.UI.V2.XYAbsFigureLocator = draw2d.layout.locator.PortLocator.extend({
	
    NAME : "Rainier.UI.V2.XYAbsFigureLocator",
    
    /**
	 * 函数名称：Rainier.UI.V2.XYAbsFigureLocator.init<br>
	 * 函数功能：构造函数<br>
	 * 参数输入：<br>
	 * x:Number,
	 * y:Number
	 * 异常信息：无<br>
	 */
    init : function(x ,y){
      this._super();
      
      this.x = x;
      this.y = y;
      
      this.oldWidth = null;
      this.oldHeight = null;
    },
    
    /**
	 * 函数名称：Rainier.UI.V2.XYAbsFigureLocator.applyConsiderRotation<br>
	 * 函数功能：复写父类函数<br>
	 */
    applyConsiderRotation : function(figure, x, y){
        var parent = figure.getParent();
    
        var halfW = 0.0;
        var halfH = 0.0;
        
        var parentWidth  = parent.getWidth();
        var parentHeight = parent.getHeight();
        var rotAngle = parent.getRotationAngle();   
        
        if(figure instanceof draw2d.shape.basic.Circle){
            halfW = parentWidth/2;
            halfH = parentHeight/2;
        }else{
            //修改元件旋转后引脚位置偏离
            // if(rotAngle=== 90|| rotAngle===270){
            //     halfW = (parentWidth-figure.getMinHeight())/2;
            //     halfH = (parentHeight-figure.getMinWidth())/2;
            // }
            //else{
                halfW = (parentWidth-figure.getMinWidth())/2;
                halfH = (parentHeight-figure.getMinHeight())/2;
            //}
        }
            
        var m = Raphael.matrix();
        m.rotate(rotAngle, halfW, halfH);  
        if(rotAngle=== 90|| rotAngle===270){
            var ratio = parentHeight/parentWidth;
            m.scale(ratio, 1/ratio, halfW, halfH);
        }
        var _x = m.x(x,y);
        var _y = m.y(x,y);
        figure.setPosition(_x, _y);
    },
    
    /**
	 * 函数名称：Rainier.UI.V2.XYAbsFigureLocator.relocate<br>
	 * 函数功能：重新计算坐标位置<br>
	 * 函数返回：无<br>
	 * 异常信息：无<br>
	 */
    relocate : function(index,figure){
        var node = figure.getParent();
        var width = node.getWidth();
        var height = node.getHeight();
        var rotAngle = node.getRotationAngle(); 
        var ratioW = 1;
        var ratioH = 1;
        var rotate = false;
        if(rotAngle=== 90|| rotAngle===270){
        	rotate = true;
        }
        if(!this.oldWidth && !this.oldHeight){
        	this.oldWidth = rotate?height:width;
        	this.oldHeight = rotate?width:height;
        }
        if(rotate){
        	ratioW = width/this.oldWidth;
        	ratioH = height/this.oldHeight;
        }
        var x = ratioW * this.x;
        var y = ratioH  * this.y;

        this.applyConsiderRotation( figure, x, y);
        //this.applyConsiderRotation(figure,this.x,this.y);
    }
});